/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.JobGroup;
import com.thinkgem.jeesite.modules.cms.service.JobGroupService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 用户岗位组记录Controller
 * @author leo
 * @version 2017-02-25
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/jobGroup")
public class JobGroupController extends BaseController {

	@Autowired
	private JobGroupService jobGroupService;
	
	@ModelAttribute
	public JobGroup get(@RequestParam(required=false) String id) {
		JobGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = jobGroupService.get(id);
		}
		if (entity == null){
			entity = new JobGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:jobGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(JobGroup jobGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		String jcdept = "";
		String roleEnName = "";
		try{
			jcdept = UserUtils.getOfficeList().get(0).getAddress();
			//jcdept = UserUtils.getUser().getOffice().getAddress();
			roleEnName = UserUtils.getUser().getRoleEnNames();
		}catch(Exception e){
			e.printStackTrace();
			jcdept = "";
		}
		Page<JobGroup> page = null;
		if (roleEnName.contains("workshop")){
			if (jcdept != null &&  jcdept.isEmpty() == false){
				jobGroup.setJcdeptid(jcdept);
			}
			page = jobGroupService.findPageLimit(new Page<JobGroup>(request, response), jobGroup);
		}else{
			page = jobGroupService.findPage(new Page<JobGroup>(request, response), jobGroup);  
		}
		model.addAttribute("page", page);
		model.addAttribute("jcdept", jcdept);
		return "modules/cms/jobGroupList";
	}

	@RequiresPermissions("cms:jobGroup:view")
	@RequestMapping(value = "form")
	public String form(JobGroup jobGroup, Model model) {
		String jcdept = "";
		try{
			jcdept = UserUtils.getUser().getOffice().getAddress();
		}catch(Exception e){
			e.printStackTrace();
		}
		model.addAttribute("jobGroup", jobGroup);
		model.addAttribute("jcdept", jcdept);
		return "modules/cms/jobGroupForm";
	}

	@RequiresPermissions("cms:jobGroup:edit")
	@RequestMapping(value = "save")
	public String save(JobGroup jobGroup, Model model, RedirectAttributes redirectAttributes) {
		String jcdept = "";
		String roleEnName = "";
		try{
			jcdept = UserUtils.getOfficeList().get(0).getAddress();
			//jcdept = UserUtils.getUser().getOffice().getAddress();
			roleEnName = UserUtils.getUser().getRoleEnNames();
		}catch(Exception e){
			e.printStackTrace();
			jcdept = "";
		}
		if (!beanValidator(model, jobGroup)){
			return form(jobGroup, model);
		}
		if (roleEnName.contains("workshop")  && jobGroup.getJcdeptid().equals(jcdept) ==false){
			return form(jobGroup, model);
		}
		try {
			jobGroupService.save(jobGroup);
			addMessage(redirectAttributes, "保存信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "保存信息不成功");
		}
		
		return "redirect:"+Global.getAdminPath()+"/cms/jobGroup/?repage";
	}
	
	@RequiresPermissions("cms:jobGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(JobGroup jobGroup, RedirectAttributes redirectAttributes) {
		jobGroupService.delete(jobGroup);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.getAdminPath()+"/cms/jobGroup/?repage";
	}

}